package com.talent.jiwen;

import android.app.Dialog;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.talent.jiwen.base.BaseFragment;
import com.talent.jiwen.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen.http.common.ApiFactory;
import com.talent.jiwen.http.common.HttpUtil;
import com.talent.jiwen.http.common.ProgressSubscriber;
import com.talent.jiwen.http.result.ReadGradeListResult;
import com.talent.jiwen.my.WebShowActivity;
import com.talent.jiwen.training.data.IdNameBean;
import com.talent.jiwen.ui.widgets.ChooseIdNameDialog;
import com.talent.jiwen.util.SPConstant;
import com.talent.jiwen.util.SPUtil;
import com.talent.jiwen.util.Validators;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observable;

import static com.talent.jiwen.my.WebShowActivity.WEB_BOOK_READ;
import static com.talent.jiwen.my.WebShowActivity.WEB_CY_GAME;

public class LearnFragment extends BaseFragment {
    @BindView(R.id.bookReadLayout)
    RelativeLayout bookReadLayout;
    @BindView(R.id.gradeTv)
    TextView gradeTv;
    @BindView(R.id.cyGameLayout)
    RelativeLayout cyGameLayout;
    @BindView(R.id.selectGradeLayout)
    RelativeLayout selectGradeLayout;

    private List<IdNameBean> idNameBeanList = new ArrayList<>();

    @Override
    protected void initData() {

        getGradeList();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_learn;
    }


    @OnClick({R.id.selectGradeLayout, R.id.bookReadLayout , R.id.cyGameLayout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.selectGradeLayout:
                if (!Validators.isEmpty(idNameBeanList)){
                    ChooseIdNameDialog dialog = new ChooseIdNameDialog(getActivity(), new ChooseIdNameDialog.OnTagClickListener() {
                        @Override
                        public void onClick(Dialog dialog, IdNameBean idNameBean) {
                            gradeTv.setText(idNameBean.getName());
                            //保存选中的年级ID
                            SPUtil.intIn(SPConstant.SELECT_READ_BOOK_GRADE , idNameBean.getId());
                        }
                    });
                    dialog.show();

                    dialog.setData(idNameBeanList);
                    if(SPUtil.intOut(SPConstant.SELECT_READ_BOOK_GRADE) != 0){
                        dialog.setTagSelect(SPUtil.intOut(SPConstant.SELECT_READ_BOOK_GRADE));
                    }
                }else{
                    showToast("暂无可使用年级");
                }


                break;
            case R.id.bookReadLayout:
                if(SPUtil.intOut(SPConstant.SELECT_READ_BOOK_GRADE) == 0){
                    showToast("请先选择年级");
                }else{
                    WebShowActivity.WEB_SHOW_TYPE = WEB_BOOK_READ;
                    intentActivity(WebShowActivity.class);
                }

                break;
            case R.id.cyGameLayout:
                WebShowActivity.WEB_SHOW_TYPE = WEB_CY_GAME;
                intentActivity(WebShowActivity.class);
                break;
        }
    }


    /**获取点读年级并显示
     *
     */
    private void getGradeList(){

        Observable observable = ApiFactory.getEducationApiSingleton().getReadGradeList(SPUtil.stringOut(SPConstant.TOKEN));
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<ReadGradeListResult>(getActivity()) {
            @Override
            protected void _onNext(ReadGradeListResult readGradeListResult) {

                int selectGrade = SPUtil.intOut(SPConstant.SELECT_READ_BOOK_GRADE);
                String selectGradStr = "选择年级";

                if (!Validators.isEmpty(readGradeListResult.getGradeList())){
                    idNameBeanList.clear();

                    for(ReadGradeListResult.ReadGrade readGrade :readGradeListResult.getGradeList()){
                        IdNameBean idNameBean = new IdNameBean(readGrade.getBookId() , readGrade.getGradeName());
                        idNameBeanList.add(idNameBean);
                        if(selectGrade == readGrade.getBookId()){
                            selectGradStr = readGrade.getGradeName();
                        }
                    }

                }

                gradeTv.setText(selectGradStr);

            }

            @Override
            protected void _onError(String message) {
                showToast(message);
            }
        }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject,false);
    }
}
